#!/bin/bash
#该脚本需要在root权限下运行
#------------------------------------------------------

#获取该脚本所在地址
MYDIR=$(cd "$(dirname "$0")";pwd)

#获取桌面端还是服务端
TYPE=$1

if [ "${TYPE}" == "server" ] || [ "${TYPE}" == "ts_server" ]; then
  FLAG="server"
elif [ "${TYPE}" == "desktop" ] || [ "${TYPE}" == "ts_desktop" ]; then
  FLAG="desktop"
fi

#PACKAGES=$(cat rpm-server.dep )
PACKAGES=$(cat rpm-$FLAG.dep )

#安装GCBS所需依赖并打开rpcbind
dependencePackage(){
	#GCBS依赖
	i=0
	if [ -f ${MYDIR}/install_packages.log ];then
		rm ${MYDIR}/install_packages.log
	fi

	echo `date "+安装日期：%Y-%m-%d时间：%H:%M:%S"` >> "${MYDIR}/install_packages.log"

	for line in $PACKAGES ;do
		((i++))
		PACKAGE=$line
		PACKAGE[$i]=$PACKAGE
		rpm -q ${PACKAGE[i]}
		ret=$?
		if (( $ret == 1 ));then
			yum install -y ${PACKAGE[i]}
			rpm -q ${PACKAGE[i]}
			if (( $? == 1 ));then
				yum install -y ${PACKAGE[i]}
				rpm -q ${PACKAGE[i]}
				if (( $? == 1 ));then
					echo "ERROR: the ${PACKAGE[i]} name is incorrect or requires manual installation" >> "${MYDIR}/install_packages.log"
					info=$(sudo yum -y install ${PACKAGE[i]})
					echo -e "error occur $info \n" >> "${MYDIR}/error_packages.log"
				else
					echo "${PACKAGE[i]}: had installed rightly again" >> "${MYDIR}/install_packages.log"
				fi
			else
				echo "${PACKAGE[i]}: had installed rightly" >> "${MYDIR}/install_packages.log"
			fi
		elif (( $ret == 2 ));then
			echo "ERROR: Incorrect usage: yum install -y ${PACKAGE[i]}" >> "${MYDIR}/install_packages.log"
		elif (( $ret == 126 ));then
			echo "ERROR: Not an executable: yum install -y ${PACKAGE[i]}" >> "${MYDIR}/install_packages.log"
		elif (( $ret == 127 ));then
			echo "ERROR: Command not found: yum install -y ${PACKAGE[i]}" >> "${MYDIR}/install_packages.log"
		elif (( $ret ==  0  ));then
			echo "${PACKAGE[i]}: had installed" >> "${MYDIR}/install_packages.log"
		else
			echo "ERROR: ${PACKAGE[i]}: unknown code[$ret]" >> "${MYDIR}/install_packages.log"
		fi
	done
	
}

#rpc
FixRpc(){

	 File=/usr/include/rpc/rpc.h
        if [ ! -f "$File" ]; then
             echo "FixRpc......"
                ln -s /usr/include/tirpc/rpc/rpc.h /usr/include/rpc/rpc.h
                ln -s /usr/include/tirpc/rpc/types.h /usr/include/rpc/types.h
                ln -s /usr/include/tirpc/rpc/xdr.h /usr/include/rpc/xdr.h
                ln -s /usr/include/tirpc/rpc/auth.h /usr/include/rpc/auth.h
                ln -s /usr/include/tirpc/rpc/clnt_stat.h /usr/include/rpc/clnt_stat.h
                ln -s /usr/include/tirpc/rpc/clnt.h /usr/include/rpc/clnt.h
                ln -s /usr/include/tirpc/rpc/clnt_soc.h /usr/include/rpc/clnt_soc.h
                ln -s /usr/include/tirpc/rpc/rpc_msg.h /usr/include/rpc/rpc_msg.h
                ln -s /usr/include/tirpc/rpc/auth_unix.h /usr/include/rpc/auth_unix.h
                ln -s /usr/include/tirpc/rpc/auth_des.h /usr/include/rpc/auth_des.h
                ln -s /usr/include/tirpc/rpc/svc_auth.h /usr/include/rpc/svc_auth.h
                ln -s /usr/include/tirpc/rpc/svc.h /usr/include/rpc/svc.h
                ln -s /usr/include/tirpc/rpc/svc_soc.h /usr/include/rpc/svc_soc.h
                ln -s /usr/include/tirpc/rpc/pmap_clnt.h /usr/include/rpc/pmap_clnt.h
                ln -s /usr/include/tirpc/rpc/pmap_prot.h /usr/include/rpc/pmap_prot.h
                ln -s /usr/include/tirpc/rpc/rpcb_clnt.h /usr/include/rpc/rpcb_clnt.h
                ln -s /usr/include/tirpc/rpc/rpcb_prot.h /usr/include/rpc/rpcb_prot.h
                ln -s /usr/include/tirpc/rpc/svc_mt.h /usr/include/rpc/svc_mt.h
                ln -s /usr/include/tirpc/rpc/rpcent.h /usr/include/rpc/rpcent.h
                ln -s /usr/include/tirpc/netconfig.h /usr/include/netconfig.h 
        fi

}

FixJava(){
        FILE="text.txt"
        java -version &> $FILE
        info=`cat -n $FILE | head -n 1 | tail -n 1`
        sstr=`echo $info | cut -d \" -f 2`
        echo $sstr
        FILEJAVA="testjava.txt"
        javac -version &> $FILEJAVA
        infojava=`cat -n $FILEJAVA | head -n 1 | tail -n 1`
        var3=`echo "$infojava"|awk -F ' ' '{print $1}'`
        if [ "$var3" != "$sstr" ];then
                echo java version is not equal
                yum remove java*
                yum remove *jdk
                yum install java-devel
        fi
	rm $FILE $FILEJAVA
}

#主函数
EnvPrepare(){	

	#安装GCBS所需依赖
	dependencePackage

	if [ "${TYPE}" == "server" ]; then
		./prepare-rpm-virt.sh
	fi

	#打开rpcbind
	rpcbind

	#判断是否需要统一JAVA版本
	FixJava

	FixRpc
}

EnvPrepare

